package com.lanou.tk.tourism.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.entity.World;

import java.util.List;


/**
 * Created by dllo on 15/11/4.
 */
public class HotCountryRecyclerViewAdapter extends RecyclerView.Adapter<HotCountryRecyclerViewAdapter.HotCountryViewHolder>{
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private List<World.DataEntity.HotCountryEntity> hotCountryEntities;

    public HotCountryRecyclerViewAdapter(Context c) {
        queue = Volley.newRequestQueue(c);
        lruCache = new LruCache<String, Bitmap>(20);
        imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url, bitmap);
            }
        };
        imageLoader = new ImageLoader(queue, imageCache);
    }

    public void addData(List<World.DataEntity.HotCountryEntity> datas) {
        hotCountryEntities = datas;
        notifyDataSetChanged();
    }

    @Override
    public HotCountryViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.fragment_target_recycler_view_iten, null);
        return new HotCountryViewHolder(v);
    }

    @Override
    public void onBindViewHolder(HotCountryViewHolder holder, int position) {
        if (hotCountryEntities != null && hotCountryEntities.size() > 0) {
            World.DataEntity.HotCountryEntity wdh = hotCountryEntities.get(position);

            holder.CountryCNNTv.setText(wdh.getCnname());
            holder.CountryENNTv.setText(wdh.getEnname());
            holder.CityNameTv.setText(wdh.getLabel());
            holder.CityCountTv.setText(wdh.getCount() + "");

            String imgUrl = wdh.getPhoto();
            getBitmapCache(imgUrl, holder.countryImgIv);
        }
    }

    private void getBitmapCache(String url, ImageView imageView) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(
                imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher
        ); imageLoader.get(url, listener);
    }

    @Override
    public int getItemCount() {
        return hotCountryEntities != null && hotCountryEntities.size() > 0 ? hotCountryEntities.size() : 0;
    }

    public class HotCountryViewHolder extends RecyclerView.ViewHolder{
        private ImageView countryImgIv;
        private TextView CityCountTv, CityNameTv, CountryCNNTv, CountryENNTv;

        public HotCountryViewHolder(View v) {
            super(v);
            countryImgIv = (ImageView) v.findViewById(R.id.countryImgIv);
            CityCountTv = (TextView) v.findViewById(R.id.CityCountTv);
            CityNameTv = (TextView) v.findViewById(R.id.CityNameTv);
            CountryCNNTv = (TextView) v.findViewById(R.id.CountryCNNTv);
            CountryENNTv = (TextView) v.findViewById(R.id.CountryENNTv);
        }
    }
}
